package com.wskeee.ec.containers
{
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class GridBox extends Box
	{
		protected var _columns:Number = 4;
		protected var _itemWidth:Number;
		protected var _itemHeight:Number;
		protected var _autoSize:Boolean = false;
		
		public function GridBox(column:int=4,itemWidth:Number=NaN,itemHeight:Number=NaN)
		{
			super();
			_columns = column;
			_itemWidth = itemWidth;
			_itemHeight = itemHeight;
		}
		//========================= validate ====================
		override protected function measure():void
		{
			super.measure();
			//当存在自定义的单元宽高并且要比最大的单元的宽高要大时，就使用自定义
			if(numChildren==0)return;
			if(!autoSize)
			{
				if(!isNaN(_itemWidth))
					maxItemWidth = Math.max(maxItemWidth,_itemWidth);
				if(!isNaN(_itemHeight))
					maxItemHeight = Math.max(maxItemHeight,_itemHeight);
				var maxClolumn:int = Math.min(numChildren,columns);
				var maxRownum:int = Math.ceil(numChildren/columns);
				measureWidth = _paddingLeft + _paddingRight + Math.max(maxClolumn-1,0)*_horizontalGap + maxClolumn*maxItemWidth;
				measureHeight = _paddingTop + _paddingBottom + Math.max(maxRownum-1,0)*_verticalGap + maxRownum*maxItemHeight;
			}else
			{
				var ypos:Number 		= _paddingTop;
				var xpos:Number 		= _paddingLeft;
				
				var heightValues:Array  = [];
				var child:DisplayObject;
				var isBr:Boolean;
				for(var i:int=0,len:int = numChildren;i<len;i++)
				{
					child = getChildAt(i);
					isBr = (i%_columns) == _columns - 1;
					heightValues.push(child.height);
					if(isBr || i == len-1)
					{
						heightValues.sort(Array.NUMERIC|Array.DESCENDING);
						measureWidth = Math.max(measureWidth,xpos + child.width);
						measureHeight = ypos + heightValues[0];
						ypos = measureHeight + _verticalGap;
						xpos = _paddingLeft;
						heightValues = [];
						if(i == len-1)
						{
							measureWidth += _paddingRight;
							measureHeight +=_paddingBottom;
						}
					}else
					{
						xpos += child.width + _horizontalGap;
					}
				}
			}
		}
		override protected function updateDisplayList():void
		{
			super.updateDisplayList();
			
			var ypos:Number 		= _paddingTop;
			var xpos:Number 		= _paddingLeft;
			
			var heightValues:Array  = [];
			var child:DisplayObject;
			var isBr:Boolean;
			
			//确定 单元 item 的大小
			var rect:Rectangle = new Rectangle(0,0,maxItemWidth,maxItemHeight);
			borderDrawer.lineStyle(0.1,borderColor as uint,0.5);
			for(var i:int = 0,len:int=numChildren; i < len; i++)
			{
				child = getChildAt(i);
				if(autoSize)
				{
					isBr = (i%_columns) == _columns - 1;
					child.x = xpos;
					child.y = ypos;
					heightValues.push(child.height);
					if(isBr || i == len-1)
					{
						heightValues.sort(Array.NUMERIC|Array.DESCENDING);
						ypos += heightValues[0] + _verticalGap;
						xpos = _paddingLeft;
						heightValues = [];
					}else
					{
						xpos += child.width + _horizontalGap;
					}
				}else
				{
					xpos = i%_columns*(rect.width + _horizontalGap)+_paddingLeft;
					ypos = Math.floor(i/_columns)*(rect.height+_verticalGap)+_paddingTop;
					if(border && !autoSize)
						borderDrawer.drawRect(xpos,ypos,rect.width,rect.height);
					child.x = xpos + getDX(rect,child.width);
					child.y = ypos + getDY(rect,child.height);
				}
			}
			if(border)
				borderDrawer.drawRect(0,0,measureWidth,measureHeight);
			
			if(oldMeasureWidth!=measureWidth || oldMeasureHeight!=measureHeight)
				dispatchEvent(new Event(Event.RESIZE));
		}	
		//========================= end =========================
		
		//========================= utils =======================
		/**
		 * [       ]
		 * [ [   ] ]
		 * [       ]
		 * 获取 在 Rect 里 以 horizontalAlign 排列时，x 的偏移量
		 * @param rect	指定大小的区
		 * @param w		物体宽
		 * @return 		
		 * 
		 */
		private function getDX(rect:Rectangle,w:Number):Number
		{
			var dx:Number = 0;
			switch(horizontalAlign)
			{
				case LayoutConst.LEFT:
					break;
				case LayoutConst.CENTER:
					dx = (rect.width - w)/2;
					break;
				case LayoutConst.RIGHT:
					dx = rect.width - w
					break;
			}
			return dx;
		}
		
		/**
		 * [       ]
		 * [ [   ] ]
		 * [       ]
		 * 获取 在 Rect 里 以 verticalAlign 排列时，y 的偏移量
		 * @param rect	指定大小的区
		 * @param h		物体高
		 * @return 		
		 * 
		 */
		private function getDY(rect:Rectangle,h:Number):Number
		{
			var dy:Number = 0;
			switch(verticalAlign)
			{
				case LayoutConst.TOP:
					break;
				case LayoutConst.MIDDLE:
					dy = (rect.height - h)/2;
					break;
				case LayoutConst.BOTTOM:
					dy = rect.height - h
					break;
			}
			return dy;
		}
		//========================= end =========================
		
		//========================= get & set ===================
		/**
		 * 设置/获取列数 
		 * @return 
		 * 
		 */
		public function get columns():Number
		{
			return _columns;
		}
		public function set columns(val:Number):void
		{
			_columns = val;
			invalidateSize();
		}	
		
		/**
		 * grid item 最大高度 
		 * @return 
		 * 
		 */
		public function get itemHeight():Number
		{
			return _itemHeight;
		}
		public function set itemHeight(value:Number):void
		{
			_itemHeight = value;
			invalidateSize();
		}
		
		/**
		 * gird item 最大宽度 
		 * @return 
		 * 
		 */
		public function get itemWidth():Number
		{
			return _itemWidth;
		}
		public function set itemWidth(value:Number):void
		{
			_itemWidth = value;
			invalidateSize();
		}
		
		/**
		 * gird item 自动适应高宽 
		 * @return 
		 * 
		 */
		public function get autoSize():Boolean
		{
			return _autoSize;
		}
		public function set autoSize(value:Boolean):void
		{
			_autoSize = value;
			invalidateSize();
		}
		//========================= end =========================
	}
}